package com.itheima.service.impl;

import com.itheima.domain.Orders;
import com.itheima.factory.MapperFactory;
import com.itheima.mapper.OrdersMapper;
import com.itheima.service.OrdersService;
import com.itheima.utils.PageUtil;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;
import org.apache.poi.ss.formula.functions.T;

import java.util.List;

public class OrdersServiceImpl implements OrdersService {
    @Override
    /*带分页查询所有*/
    public List<Orders> findAll(String pageNum, String pageSize) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        OrdersMapper mapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
        PageUtil.page(pageNum, pageSize);
        List<Orders> list = mapper.findAll();
        TransactionUtil.close(sqlSession);
        return list;
    }

    @Override
    public void ordersAdd(Orders orders) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        OrdersMapper mapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
        try {
            mapper.ordersAdd(orders);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
        }finally {
            TransactionUtil.close(sqlSession);
        }


    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        OrdersMapper mapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
        try {
            mapper.delete(ids);
            TransactionUtil.commit(sqlSession);
        }catch (Exception e){
            TransactionUtil.rollback(sqlSession);
        }finally {
            TransactionUtil.close(sqlSession);
        }
    }

    @Override
    public Orders findById(String id) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        OrdersMapper mapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
        Orders orders = mapper.findById(id);
        System.out.println(orders);
        TransactionUtil.close(sqlSession);
        return orders;
    }

    @Override
    public List<Orders> findLikeName(String name, String pageNum, String pageSize) {
        SqlSession sqlSession = MapperFactory.getSqlSession();
        OrdersMapper mapper = MapperFactory.getMapper(sqlSession, OrdersMapper.class);
        PageUtil.page(pageNum, pageSize);
        List<Orders> list = mapper.findLike(name);
        TransactionUtil.close(sqlSession);
        return list;
    }


}
